<?php

	/*
		Class Name: userAccount
		Description: Manages user Account Operations
		Created On: March 16, 2014
		Created By: Lenin Rodríguez
		History:		
	*/
	
	require_once ('../libs/dbConnector.php');
	require_once ('../businessObjects/rtmsEvent.php');	
	require_once ('../businessObjects/bsObjectMessages.php');	
	require_once ('../businessObjects/sysConfigParameters.php');	
	
	abstract class userAccountState{
			const active = 1;
			const blocked = 2;
		}	
	class userAccount {
			var $dbConn;
			var $message;
			var $userSaltKeyword;
			var $resultSet;			
			function userAccount(){
					$this->userSaltKeyword = '@123456P';
			}
			function message(){return $this->message;}
			function resultSet() {return $this->resultSet;}			
			function userAccountLogin($userName, $userPassword){				
				$dbConn = new dbConnector();				
				$event = new rtmsEvent();
				$boolres = false;
				
				if ($dbConn->openConnection()){													   
					$query = 'select * from useraccount where username = \''.$userName
								   .'\' and userpassword = \''. crypt($userPassword,$this->userSaltKeyword)
								   .'\' and useraccountstate = '. userAccountState::active;
					$dbConn->query($query);			
					$rows = mysqli_num_rows($dbConn->queryResults());
					if ($rows >= 1){
						$boolres = true;
						$query = 'update useraccount set lastLogin = now() where username = \''.$userName.'\'';
						$dbConn->query($query);
						$event->addEvent($userName,sysConfigParameters::sysConfiguredEnvironment,date("Y-m-d H:i:s"),$userName.' LOGGED IN',rtmsEventLevel::information);
						}
					else {
						$this->message = bsObjectMessages::user_credentials_dont_match;
						$boolres = false;
						}	
				}else{
					$this->message = bsObjectMessages::something_went_wrong.$dbConn->message();
					$boolres = false;
				}
				$dbConn->closeConnection();
				return $boolres;
			}
			function userAccountInformation($username){
				$dbConn = new dbConnector();				
				$boolres = false;
				
				if ($dbConn->openConnection()){													   
					$query = 'select '.
							  'ua.username as username, '.
							  'uas.statename as userAccountState,'.
							  'ua.lastlogin,'.
							  'e.firstName,'.
							  'e.lastName,'.
							  'ee.enterpriseID,'.
							  'ee.enterpriseName'.
							 ' from useraccount ua, useraccountstate uas, employee e, enterprise ee'.
							 ' where '.
							 ' ua.useraccountstate = uas.useraccountstateid '.
							 ' and ua.employeeID = e.employeeID'.
							 ' and e.enterpriseID = ee.enterpriseID'.
							 ' and ua.username =\''.$username.'\'';					
					$dbConn->query($query);
					$this->resultSet = $dbConn->queryResults();					
					$boolres = true;
				}else{
					$this->message = bsObjectMessages::something_went_wrong.$dbConn->message();
					$boolres = false;
				}
				$dbConn->closeConnection();
				return $boolres;
			}			
			function updateUserPassword ($userName, $userOldPassword, $userNewPassword){								
				$dbConn = new dbConnector();
				$event = new rtmsEvent();				
				$boolres = false;				
				if ($dbConn->openConnection()){													   
					$query = 'select * from useraccount where username = \''.$userName.'\' and userpassword=\''.crypt($userOldPassword,$this->userSaltKeyword).'\'';					
					$dbConn->query($query);
					$rows = mysqli_num_rows($dbConn->queryResults());
					if ($rows >= 1){						
						$query = 'update useraccount set userpassword = \''.crypt($userNewPassword,$this->userSaltKeyword)
						.'\' where username = \''.$userName.'\'';
						$dbConn->query($query);
						$this->message = bsObjectMessages::user_updated_successfully;
						$event->addEvent($userName,sysConfigParameters::sysConfiguredEnvironment,date("Y-m-d H:i:s"),$userName.' updated user account information.',rtmsEventLevel::information);
						$boolres = true;
					}else
					{
						$this->message = bsObjectMessages::user_credentials_dont_match;
					}					
				}else{
					$this->message = bsObjectMessages::something_went_wrong.$dbConn->message();
					$boolres = false;
				}
				$dbConn->closeConnection();
				return $boolres;
			}
			
			function userAccountLogOut($userName){
				$dbConn = new dbConnector();				
				$event = new rtmsEvent();
				$boolres = false;
				if ($dbConn->openConnection()){													   
					$query = 'select * from useraccount where username = \''.$userName.'\'';	
					$dbConn->query($query);			
					$rows = mysqli_num_rows($dbConn->queryResults());
					if ($rows >= 1){
						$boolres = true;
						$event->addEvent($userName,sysConfigParameters::sysConfiguredEnvironment,date("Y-m-d H:i:s"),$userName.' LOGGED OUT',rtmsEventLevel::information);
						}
					else {
						$this->message = bsObjectMessages::user_credentials_dont_match;
						$boolres = false;
						}	
				}else{
					$this->message = bsObjectMessages::something_went_wrong.$dbConn->message();
					$boolres = false;
				}
				$dbConn->closeConnection();
				return $boolres;
			}
		}
?>